package com.shell.context.utils;

import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.cache.Cache;
import com.shell.bee.cache.CacheBuilder;
import com.shell.constant.ind.PubConstants;
import com.shell.constant.ind.PubConstants.CacheContextType;
import com.shell.context.service.ContextService;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ContextCache {
	protected static final Log logger = LogFactory.getLog(ContextCache.class);
	public static final String CACHE_CONTEXT_KEY_PREFIX = "CONTEXT_DATA_CACHE_";
	private static Cache<String, Map<String, Object>> CACHE;

	public static Map<String, Object> getValue() {
		if (CACHE == null) {
			CACHE = CacheBuilder.newBuilder().build();
		}
		Map<String, Object> context = new HashMap();
		try {
			List<String> keys = getSystemCacheContextKeys();
			Map<String, Object> tmpMap = null;
			for (String key : keys) {
				if (CACHE.exists(key)) {
					context.putAll((Map) CACHE.get(key));
				} else {
					tmpMap = getCacheValue(key);
					if (tmpMap != null) {
						context.putAll(tmpMap);
					}
				}
			}
		} catch (Exception e) {
			logger.error("============ �������������������� ============");
			e.printStackTrace();
		}
		return context;
	}

	public static Map<String, Object> refresh() {
		cleanCache();
		return getValue();
	}

	public static Map<String, Object> refresh(String key) {
		if (CACHE == null) {
			CACHE = CacheBuilder.newBuilder().build();
		}
		if (CACHE.exists(key)) {
			CACHE.remove(key);
		}
		return getCacheValue(key);
	}

	public static Map<String, Object> getCacheValue(String key) {
		Map<String, Object> tmpMap = null;
		try {
			ContextService contextService = (ContextService) SpringContextUtil.getBean("contextService");
			if ("CONTEXT_DATA_CACHE_CONSTANT".equals(key)) {
				tmpMap = contextService.getConstantsData();
			} else if ("CONTEXT_DATA_CACHE_DICTIONARY".equals(key)) {
				tmpMap = contextService.getDictionaryData();
			} else if ("CONTEXT_DATA_CACHE_PARAMETER".equals(key)) {
				tmpMap = contextService.getParameterData();
			} else if ("CONTEXT_DATA_CACHE_APPLICATIONS".equals(key)) {
				tmpMap = contextService.getApplicationsData();
			} else if ("CONTEXT_DATA_CACHE_SWITCHS".equals(key)) {
				tmpMap = contextService.getSwitchs();
			}
			if (tmpMap != null) {
				CACHE.put(key, tmpMap);
			}
		} catch (Exception e) {
			logger.error("============ �������������������� ============");
			e.printStackTrace();
		}
		return tmpMap;
	}

	public static List<String> getSystemCacheContextKeys() {
		List<String> list = new ArrayList();

		Class classz = PubConstants.CacheContextType.class;
		Field[] fields = classz.getDeclaredFields();
		for (Field f : fields) {
			try {
				list.add("CONTEXT_DATA_CACHE_" + (String) f.get(f.getName()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static void cleanCache() {
		if (CACHE == null) {
			CACHE = CacheBuilder.newBuilder().build();
		}
		List<String> keys = getSystemCacheContextKeys();
		for (String key : keys) {
			if (CACHE.exists(key)) {
				CACHE.remove(key);
			}
		}
	}
}
